package com.cari.oa.dao;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import com.cari.oa.domain.SoftRiskEntity;
import com.cari.oa.util.StrUtils;

/**
 * 
* Title: SoftRiskDao
* Description:风险管理dao	
* Company: cari 
* @author    hjx 
* @date       2013-1-21
 */
@Repository
public class SoftRiskDao extends BaseDao<SoftRiskEntity> {
	
	
    /**
     * 
    * Title: getPage
    * Description:分页获取风险列表
    * @author    hjx 
    * @date       2013-1-21 
    * @param riskType
    * @param status
    * @param projectId
    * @param page
    * @param size
    * @return
     */
    public List<SoftRiskEntity> getPage(String riskType, String status,String projectId, int page, int size) {
    	
    	List<Object> params = new ArrayList<Object>();

        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("from SoftRiskEntity r where 1=1 ");
        
        if(StrUtils.isNotEmpty(riskType)) {
            stringBuilder.append("and r.riskType like ? ");
            params.add("%" + riskType + "%");
        }

        if(StrUtils.isNotEmpty(status)) {
            stringBuilder.append("and r.status like ? ");
            params.add("%" + status + "%");
        }
        
        if(StrUtils.isNotEmpty(projectId)) {
            stringBuilder.append("and r.projectId = ? ");
            params.add(projectId);
        }
        stringBuilder.append(" order by riskType ");
        return getPage(stringBuilder.toString(), page, size, params.toArray());
    }
    

   /**
    * 
   * Title: getCount
   * Description:查询的记录数
   * @author    hjx 
   * @date       2013-1-21 
   * @param riskType
   * @param status
   * @param projectId
   * @return
    */
    public long getCount(String riskType, String status,String projectId) {
    	
    	List<Object> params = new ArrayList<Object>();

        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("select count(*) from  SoftRiskEntity r where 1=1 ");
        
        if(StrUtils.isNotEmpty(riskType)) {
            stringBuilder.append("and r.riskType like ? ");
            params.add("%" + riskType + "%");
        }

        if(StrUtils.isNotEmpty(status)) {
            stringBuilder.append("and r.status like ? ");
            params.add("%" + status + "%");
        }
        
        if(StrUtils.isNotEmpty(projectId)) {
            stringBuilder.append("and r.projectId = ? ");
            params.add(projectId);
        }
        Query query = getSession().createQuery(stringBuilder.toString());
        for (int i = 0; i < params.size(); i++) {
            query.setParameter(i, params.get(i));
        }
        return (Long)query.uniqueResult();
    }
}
